package com.ruoyi.StarStudio.Service.Impl;

import com.ruoyi.StarStudio.DAO.GoodsDAO;
import com.ruoyi.StarStudio.DAO.PanicBuyingGoodsDAO;
import com.ruoyi.StarStudio.Entity.Goods;
import com.ruoyi.StarStudio.Entity.PanicBuyingGoods;
import com.ruoyi.StarStudio.Service.PanicBuyingGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class PanicBuyingGoodsServiceImpl implements PanicBuyingGoodsService {

    @Autowired
    PanicBuyingGoodsDAO panicBuyingGoodsDAO;

    @Autowired
    GoodsDAO goodsDAO;

    @Override
    public List<PanicBuyingGoods> findPanicBuyingGoodsList(PanicBuyingGoods panicBuyingGoods) {
        return panicBuyingGoodsDAO.findPanicBuyingGoodsList(panicBuyingGoods);
    }

    @Override
    public long findTotalRecord() {
        return panicBuyingGoodsDAO.findTotalRecord();
    }

    @Override
    public PanicBuyingGoods findByGoods_id(long goods_id) {
        return panicBuyingGoodsDAO.findByGoods_id(goods_id);
    }

    @Override
    public List<PanicBuyingGoods> PanicBuyingGoodsList() {
        return panicBuyingGoodsDAO.PanicBuyingGoodsList();
    }

    @Override
    public int updatePanicBuyingGoods(PanicBuyingGoods panicBuyingGoods) {
        int new_pb_goods_stock = panicBuyingGoods.getPanic_buying_goods_stock();
        int old_pb_goods_stock = panicBuyingGoodsDAO.findByPanic_buying_goods_stockByPanic_buying_goods_id(panicBuyingGoods.getPanic_buying_goods_id());
        if (new_pb_goods_stock != old_pb_goods_stock){
            Goods byGoods_id = goodsDAO.findByGoods_id(panicBuyingGoods.getGoods_id());
            int totalStock = byGoods_id.getStock() + old_pb_goods_stock;
            if (new_pb_goods_stock > old_pb_goods_stock){ /* 商品库存增加 */
                if (new_pb_goods_stock <= totalStock){
                    byGoods_id.setStock(totalStock - new_pb_goods_stock);
                }else {
                    throw new RuntimeException("商品库存不足！");
                }
            }else { /* 商品库存减少 */
                if (new_pb_goods_stock >= 0){
                    byGoods_id.setStock(totalStock - new_pb_goods_stock);
                }else {
                    throw new RuntimeException("库存不能小于零！");
                }
            }
            goodsDAO.updateGoods(byGoods_id);
        }
        return panicBuyingGoodsDAO.updatePanicBuyingGoods(panicBuyingGoods);
    }

    @Override
    public void AddPanicBuyingGoods(PanicBuyingGoods panicBuyingGoods) {
        int panic_buying_goods_stock = panicBuyingGoods.getPanic_buying_goods_stock();
        Goods byGoods_id = goodsDAO.findByGoods_id(panicBuyingGoods.getGoods_id());
        if (byGoods_id.getStock() >= panic_buying_goods_stock){
            byGoods_id.setStock(byGoods_id.getStock() - panic_buying_goods_stock);
            goodsDAO.updateGoods(byGoods_id);
            panicBuyingGoodsDAO.AddPanicBuyingGoods(panicBuyingGoods);
        }else {
            throw new RuntimeException("商品库存不足！");
        }
    }

    @Override
    public void DeletePanicBuyingGoods(long panic_buying_goods_id) {
        panicBuyingGoodsDAO.DeletePanicBuyingGoods(panic_buying_goods_id);
    }

    /**
     * 每分钟更新抢购商品状态
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void AutoUpdatePanicBuyingStatus(){
        List<PanicBuyingGoods> panicBuyingGoodsList = panicBuyingGoodsDAO.PanicBuyingGoodsList();
        if (!CollectionUtils.isEmpty(panicBuyingGoodsList)){
            for (PanicBuyingGoods panicBuyingGoods : panicBuyingGoodsList) {
                if (new Timestamp(new Date().getTime()).before(panicBuyingGoods.getPanic_buying_start_time())){
                    panicBuyingGoods.setPanic_buying_status(0);
                }else if (new Timestamp(new Date().getTime()).after(panicBuyingGoods.getPanic_buying_end_time())){
                    panicBuyingGoods.setPanic_buying_status(2);
                }else {
                    panicBuyingGoods.setPanic_buying_status(1);
                }
                panicBuyingGoodsDAO.updatePanicBuyingGoods(panicBuyingGoods);
            }
        }
    }


}
